package top.infopub.mgr.bpa.web;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.bpa.BpaPublicProInfoDto;
import top.infopub.dto.bpa.EffectPlanChargedDetailDto;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.dao.EffectPlanChargedDao;
import top.infopub.mgr.bpa.domain.EffectPlanChargedDomain;
import top.infopub.mgr.bpa.jsonVo.BizEffectChargedDetailJsonVo;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.service.EffectPlanChargedService;
import top.infopub.mgr.bpa.vo.EffectPlanPublicVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.mkt.service.ProjectInfoService;
import top.infopub.model.bpa.EffectPlanCharged;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;

/**
 *
 创效激励兑现
 */
@Controller
@RequestMapping("/effectPlanCharged")
public class EffectPlanChargedController extends GenericFlowController {

    @Autowired
    private EffectPlanChargedService effectPlanChargedService;

    @Autowired
    private EffectPlanChargedDao effectPlanChargedDao;

    @Autowired
    private BpaProcessManageService bpaProcessManageService;

    @Autowired
    private ProjectInfoService projectInfoService;

    /**
     * 跳转列表页
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        return "bpa/lay-effectPlanCharged-list";
    }

    //查询列表
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<EffectPlanChargedDomain> queryDataByCondition(EffectPlanPublicVo effectPlanPublicVo, ParamPage paramPage) {
        Page<EffectPlanChargedDomain> page = new Page<EffectPlanChargedDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = effectPlanChargedService.selectPageByDomainWhere(page,effectPlanPublicVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetail(HttpServletRequest request, String id, String proCode) {
        request.setAttribute("id", id);
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");//登录人和审批人一致使用该字段是否显示审核信息
        if (StringUtils.isNotBlank(id)) {
            EffectPlanCharged effectPlanCharged =  effectPlanChargedDao.selectByPrimaryKey(id);
            if(effectPlanCharged !=null){
            if (org.apache.commons.lang3.StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), effectPlanCharged.getProcessStatus())
                    || org.apache.commons.lang3.StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), effectPlanCharged.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (org.apache.commons.lang3.StringUtils.isNotBlank(effectPlanCharged.getProcessId()) && effectPlanCharged.getProcessId().length() > 10) {
                request.setAttribute("processId", effectPlanCharged.getProcessId());
                String auditTabFlag = bpaProcessManageService.checkShowAuditTab(effectPlanCharged.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), effectPlanCharged.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
            }
        }
        request.setAttribute("proCode", proCode);
        return "bpa/lay-EffectCreationIncentives";
    }

    //根据procode获取项目的基本信息
    @RequestMapping("/queryProjDetail")
    @ResponseBody
    public RspVo queryProjDetail(String proCode) {
        //项目的基础信息
        BizEffectChargedDetailJsonVo jsonVo = new BizEffectChargedDetailJsonVo();
        BpaPublicProInfoDto dto = projectInfoService.queryBpaProInfoByProCode(proCode);
        EffectPlanChargedDetailDto info = new EffectPlanChargedDetailDto();
        BeanUtils.copyProperties(dto, info);
        jsonVo.setInfo(info);
        return RspVo.getSuccessResponse(info);
    }

    @RequestMapping("/queryDetail")
    @ResponseBody
    public RspVo queryDetail(String id) {
        BizEffectChargedDetailJsonVo detailJson = effectPlanChargedService.getDetail(id);
        return RspVo.getSuccessResponse(detailJson);
    }

    /**
     * 创效激励-新增  0=保存，1=提交
     * @throws ParseException
     */
    @SuppressWarnings("rawtypes")
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr, Integer submitType) {

        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();

        String str = StringEscapeUtils.unescapeJava(bodyStr);
        BizEffectChargedDetailJsonVo detailJson = JSON.parseObject(str, BizEffectChargedDetailJsonVo.class);
        //String codes = codeGeneratorConfigService.generatorCode("bpa_biz_plan");
        RspVo rspVo = effectPlanChargedService.save(detailJson,submitType,vo);
        return rspVo;
    }
}
